import { createStore } from "vuex";
import axios from "axios";
const store = createStore({
  state: () => ({
    content: 123,
    name: "tom",
    age: 18,
  }),
  getters: {
    sum: (state, getters) => {
      return state.content + state.age;
      // 类似与计算属性
      //getters 参数可以使用定义的其他getter
    },
    console: () => {
      return (log) => {
        return log; //可以通过返回一个函数使得在使用getter时向其传参
      };
    },
  },
  mutations: {
    add(state) {
      state.content++;
    },
    pre(state) {
      state.content--;
    },
    setname(state, value) {
      // 第二个参数为传给函数的参数
      state.name = value;
    },
    getText(state, value) {
      state.text = value;
    },
  },
  actions: {
    async getTextAction(context) {
      const data = await (await axios.get("http://localhost:3003/news")).data;
      context.commit("getText", data); // 使用Action发送网络请求
    },
  },
});

export default store;
